<?php
/*******************************************/
/*              move conn  To  conn2           */
/*******************************************/
set_time_limit(0);
$bet_dbhost = "192.168.5.110";
$bet_dbuser = "kacino";
$bet_dbpass = "cyriac_0426";
$bet_dbname = "betDonUSWinusUTF8";
if (! $conn = @mysql_connect($bet_dbhost, $bet_dbuser, $bet_dbpass)) {
	print "連線錯誤：無法連線至資料庫主機！1";
	exit();
}
mysql_query("SET NAMES 'utf8'", $conn);
if (! @mysql_select_db($bet_dbname, $conn)) {
	print "連線錯誤：無法連線至資料庫主機！2";
	exit();
}

$bet_dbhost = "192.168.5.116";
$bet_dbuser = "kacino";
$bet_dbpass = "cyriac_0426";
$bet_dbname = "betBowUSWinusUTF8";
if (! $conn2 = @mysql_connect($bet_dbhost, $bet_dbuser, $bet_dbpass)) {
	print "連線錯誤：無法連線至資料庫主機！11";
	exit();
}
mysql_query("SET NAMES 'utf8'", $conn2);
if (! @mysql_select_db($bet_dbname, $conn2)) {
	print "連線錯誤：無法連線至資料庫主機！22";
	exit();
}

global $arrExistID;
$arrExistID = array();

function ChangeMemberCommissionToOtherSite($oldID, $newID){
	global $conn, $conn2;
	$strSQL = "select `LottoID`, `Bet_Type`, `Gamble_Type`, `Commission` from `bet_Commission` where MemID = ".$oldID;
	if($RS = mysql_query($strSQL, $conn)){
		while($Rows = mysql_fetch_object($RS)){
			$strSQL = "insert into bet_Commission(`MemID`, `LottoID`, `Bet_Type`, `Gamble_Type`, `Commission`) values('".$newID."','".$Rows -> LottoID."','".$Rows -> Bet_Type."','".$Rows -> Gamble_Type."','".$Rows -> Commission."')";
			if(!mysql_query($strSQL,$conn2)){
				print $strSQL."<br>\n";
				print mysql_error($conn2)."<br>\n";
			}
		}
	}else{
		print $strSQL."<br>\n";
		print mysql_error($conn)."<br>\n";
	}
}

function ChangeMemberLimitToOtherSite($oldID, $newID){
	global $conn, $conn2;
	$strSQL = "select `LottoID`, `Bet_Type`, `LimitType`, `LimitValue` from bet_MemberLimit where MemID = ".$oldID;
	if($RS = mysql_query($strSQL, $conn)){
		while($Rows = mysql_fetch_object($RS)){
			$strSQL = "insert into bet_MemberLimit(`MemID`, `LottoID`, `Bet_Type`, `LimitType`, `LimitValue`) values('".$newID."','".$Rows -> LottoID."','".$Rows -> Bet_Type."','".$Rows -> LimitType."','".$Rows -> LimitValue."')";
			if(!mysql_query($strSQL,$conn2)){
				print mysql_error($conn2)."<br>\n";
			}
		}
	}else{
		print $strSQL."<br>\n";
		print mysql_error($conn)."<br>\n";
	}
}

function ChangeMemberToOtherSite($ID, $ParentID, $newParentID){
	global $conn, $conn2, $arrID, $arrExistID;
	if($ParentID == 0){
		$strSQL = "select * from bet_Member where ID = ".$ID." and isLock = '0'";
	}else{
		$strSQL = "select * from bet_Member where ParentID = ".$ParentID." and isLock = '0'";
	}
	if($RS = mysql_query($strSQL, $conn)){
		print "QQ";
		while($Rows = mysql_fetch_object($RS)){
			print "WW";
			$arrF = array();
			$arrV = array();
			$oldMemID = $Rows -> ID;
			$KK = "";//($arrExistID[$oldMemID] == "yes")?"bww":"";
			while(list($k, $v) = each($Rows)){
				if($k == "ID")continue;
				$arrF[] = $k;
				switch($k){
					case "AddDate":
						$arrV[] = "'".date("Y-m-d")."'";
						break;

					case "AddTime":
						$arrV[] = "'".date("H:i:s")."'";
						break;

					case "Account":
						$arrV[] = "'".addslashes($v).$KK."'";
						break;

					case "ParentID":
						$arrV[] = "'".$newParentID."'";
						break;

					default:
						$arrV[] = "'".addslashes($v)."'";
						break;
				}
			}
			$strSQL = "insert into bet_Member(".join(",", $arrF).") values(".join(",", $arrV).")";
			//print $strSQL;
			//exit();
			if(mysql_query($strSQL, $conn2)){
				$newMemIDS = mysql_insert_id($conn2);
				print $Rows -> Account."==".$newParentID."==".$newMemIDS."<br>\n";
				flush();
				ChangeMemberCommissionToOtherSite($oldMemID, $newMemIDS);
				ChangeMemberLimitToOtherSite($oldMemID, $newMemIDS);
			}else{
				print $strSQL."<br>\n";
				print mysql_error($conn2)."<br>\n";
			}
			if($Rows -> MemberGroup != 1)ChangeMemberToOtherSite($oldMemID, $oldMemID, $newMemIDS);
		}
	}else{
		print $strSQL."<br>\n";
		print mysql_error($conn)."<br>\n";
		exit();
	}
}

function FindSameAccount($strID, $isParent = false){
	global $conn, $conn2, $arrExistID;
	$arrID = array();
	$Account = array();

	if($isParent){
		$strSQL = "select ID, Account, MemberGroup from `bet_Member` where ParentID in (".$strID.") and isLock = 0";
	}else{
		$strSQL = "select ID, Account, MemberGroup from `bet_Member` where ID in (".$strID.") and isLock = 0";
	}
	$RS = mysql_query($strSQL, $conn);
	while($Rows = mysql_fetch_object($RS)){
		$Account[] = "'".$Rows -> Account."'";
		$arrID[] = $Rows -> ID;
		$intGroup = $Rows -> MemberGroup;
		$arrID2[strtolower("M_".$Rows -> Account)] = $Rows -> ID;
	}

	$strSQL = "select ID, Account, MemberGroup from `bet_Member` where Account in (".join(",", $Account).")";
	$RS = mysql_query($strSQL, $conn2);
	while($Rows = mysql_fetch_object($RS)){
		//print "".$Rows -> Account." => ".$Rows -> MemberGroup."\n";
		//print '$arrID['.$arrID2[strtolower("M_".$Rows -> Account)].']="yes";'."\n";
		$arrExistID[$arrID2[strtolower("M_".$Rows -> Account)]] = "yes";
	}
	$strSQL = "update `bet_Member` set Account = CONCAT(Account, 'bo') where Account in (".join(",", $Account).")";
	if(!mysql_query($strSQL,$conn2)){
		print $strSQL."<br>\n";
		print mysql_error($conn2)."<br>\n";
	}

	$strID = join(",", $arrID);
	if($intGroup != 1 && sizeof($arrID) > 0)FindSameAccount($strID, true);
	return;
}
//print "ffg";
//exit();
/*ChangeMemberToOtherSite(15, 0, 12233);
ChangeMemberToOtherSite(33, 0, 12233);
ChangeMemberToOtherSite(168, 0, 12233);
ChangeMemberToOtherSite(250, 0, 12233);
ChangeMemberToOtherSite(392, 0, 12233);
ChangeMemberToOtherSite(393, 0, 12233);
ChangeMemberToOtherSite(401, 0, 12233);
ChangeMemberToOtherSite(445, 0, 12233);*/
FindSameAccount("11, 17552, 18534, 18620 , 18949, 19515, 19547");

print "111";
ChangeMemberToOtherSite(11 , 0, 0);
print "222";
ChangeMemberToOtherSite(17552 , 0, 0);
print "333";
ChangeMemberToOtherSite(18534 , 0, 0);
print "444";
ChangeMemberToOtherSite(18620 , 0, 0);
print "555";
ChangeMemberToOtherSite(18949 , 0, 0);
print "666";
ChangeMemberToOtherSite(19515 , 0, 0);
print "777";
ChangeMemberToOtherSite(19547 , 0, 0);
?>